home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SPACE 2
/
SPACE - Library 2 - Volume 1.iso
/
utility
/
533
/
kwic
/
qucksrt2.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1991-04-25
|
3KB
|
83 lines
{ *** Sort the array 'Arr' in situ *** }
PROCEDURE QuickSort(VAR Arr:T141; UB:T158); {T158 is integer (bounds)}
{Sorts 10000 integers in about 5.6 s.}
PROCEDURE Swap(VAR x,y:T383); {T383 is string[11]. Key words.}
VAR
Temp : T383;
BEGIN
Temp := x;
x := y;
y := Temp;
END{swap};
PROCEDURE Swap2(VAR c,d:integer);
VAR Temp:integer;
BEGIN{swap2}
Temp := c;
c := d;
d := Temp;
END{swap2};
PROCEDURE SubSort(L,R:T158);
VAR
i,j : T158;
PROCEDURE PartitionArray(L,R:T158);
{Bad name, this actually sorts. Establish a 'pivot point' with
half the items on the left, half on the right. Start at the left
and go utill an item greater than the pivot point item is found.
Stop and do the complementary thing on the right hand side.
Exchange these two items.
Continue this process until the two pointers meet.}
VAR
PivotValue : T383; {string[11]}
BEGIN
i := L;
j := R;
PivotValue := Arr[(L + R) DIV 2];
REPEAT
WHILE Arr[i] < PivotValue DO
i := i + 1;
WHILE Arr[j] > PivotValue DO
j := j - 1;
IF i < j
THEN
BEGIN
Swap(Arr[i], Arr[j]);
Swap2(TheLine[i], TheLine[j]);
i := i + 1;
j := j - 1;
END;
UNTIL i >= j;
END {partitionarray};
BEGIN{subsort}
IF L < R
THEN
IF L + 1 = R
THEN
IF Arr[L] > Arr[R]
THEN
BEGIN
Swap(Arr[L], Arr[R]);
Swap2(TheLine[L], TheLine[R]);
END
ELSE { }
ELSE {L + 1 <> R}
IF L + 1 < R
THEN {segment has 3 or more components}
BEGIN
PartitionArray(L,R);
SubSort(L,J); (* recursion *)
SubSort(I,R);
END
ELSE {L + 1 = R}
ELSE {L >= R};
END{subsort};
BEGIN {quicksort}
SubSort(1,UB);
END{quicksort};